package com.jy.horny.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import com.jy.horny.R
import com.jy.horny.ToastUtil

/**
 * 编辑昵称
 */
class EditNickNameDialog private constructor(context: Context, theme: Int) :
    Dialog(context, theme) {

    override fun dismiss() {
        super.dismiss()
    }

    interface Callback {
        fun onBackClick(name: String?)
    }

    private class Params {
        lateinit var context: Context
        var hasShadow = true
        var canCancel = true
        var nickname: String? = null
        var callback: Callback? = null
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.context = context
        }

        fun setCallback(callback: Callback, nickname: String?): Builder {
            p.callback = callback
            p.nickname = nickname
            return this
        }

        fun create(): EditNickNameDialog {
            val dialog = EditNickNameDialog(
                p.context,
                if (p.hasShadow) com.lib.module_res.R.style.Theme_Light_NoTitle_Dialog else com.lib.module_res.R.style.Theme_Light_NoTitle_NoShadow_Dialog
            )

            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(com.lib.module_res.R.style.Animation_Bottom_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
                window.attributes = lp
                window.setGravity(Gravity.CENTER)
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.context)
                .inflate(R.layout.dialog_edit_nickname, null)

            initialViews(view, dialog)

            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }

        fun show() {
            create().show()
        }

        private fun initialViews(view: View, dialog: EditNickNameDialog) {
            val editEditNickNameInput = view.findViewById<EditText>(R.id.editEditNickNameInput)
            val txtEditNickNameCancel = view.findViewById<TextView>(R.id.txtEditNickNameCancel)
            val txtEditNickNameSave = view.findViewById<TextView>(R.id.txtEditNickNameSave)
            p.nickname?.let {
                editEditNickNameInput.setText(it)
            }
            txtEditNickNameSave.setOnClickListener {
                if (TextUtils.isEmpty(editEditNickNameInput.text.toString())) {
                    dialog.dismiss()
                } else {
                    if(editEditNickNameInput.text.toString().length>30){
                        ToastUtil.showToast("Nickname cannot exceed 30 characters")
                        return@setOnClickListener
                    }
                    p.callback?.onBackClick(editEditNickNameInput.text.toString())
                    dialog.dismiss()
                }
            }
            txtEditNickNameCancel.setOnClickListener {
                dialog.dismiss()
            }
        }
    }

}